package 面试题23_链表中环的入口节点;

/**
 * @Author ：xu_xiaofeng.
 * @Date ：Created in 10:21 2021/3/11
 * @Description：
 */

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}

public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head == null) {
            return false;
        }

        ListNode pSlow = head.next;
        ListNode pFast = null;
        if (pSlow != null) {
            pFast = pSlow.next;
        }

        while (pFast != null && pSlow != null) {
            if (pFast == pSlow) {
                return true;
            }

            pSlow = pSlow.next;

            pFast = pFast.next;
            if (pFast != null) {
                pFast = pFast.next;
            }

        }

        return false;
    }
}
